// Copyright (c) 2025, Your Company Pvt. Ltd.
// License: GNU General Public License v3. See license.txt

frappe.query_reports["stock_stay"] = {
    filters: [
        {
            fieldname: "company",
            label: __("Company"),
            fieldtype: "Link",
            options: "Company",
            default: frappe.defaults.get_user_default("Company"),
            reqd: 1,
        },
        {
            fieldname: "to_date",
            label: __("As On Date"),
            fieldtype: "Date",
            default: frappe.datetime.get_today(),
            reqd: 1,
            description: __("Stock will be checked against this date to determine if it's stagnant (older than 3 months)")
        },
        {
            fieldname: "warehouse_type",
            label: __("Warehouse Type"),
            fieldtype: "Link",
            width: "80",
            options: "Warehouse Type",
        },
        {
            fieldname: "warehouse",
            label: __("Warehouse"),
            fieldtype: "Link",
            options: "Warehouse",
            get_query: () => {
                let warehouse_type = frappe.query_report.get_filter_value("warehouse_type");
                let company = frappe.query_report.get_filter_value("company");
                return {
                    filters: {
                        ...(warehouse_type && { warehouse_type }),
                        ...(company && { company }),
                    },
                };
            },
        },
        {
            fieldname: "item_code",
            label: __("Item"),
            fieldtype: "Link",
            options: "Item",
        },
        {
            fieldname: "brand",
            label: __("Brand"),
            fieldtype: "Link",
            options: "Brand",
        },
        {
            fieldname: "show_warehouse_wise_stock",
            label: __("Show Warehouse-wise Stock"),
            fieldtype: "Check",
            default: 0,
        },
        {
            fieldname: "y_axis_field",
            label: __("Y Axis Field"),
            fieldtype: "Select",
            options: ["item_name", "item_code"].join("\n"),
            default: "item_name",
            description: __("Select category field for Y axis (e.g., Item Name)")
        },
        {
            fieldname: "x_axis_field",
            label: __("X Axis Field"),
            fieldtype: "Select",
            options: ["stagnant_qty", "total_qty"].join("\n"),
            default: "stagnant_qty",
            description: __("Select value field for X axis (e.g., Stagnant Qty)")
        }
    ],
    onload: function(report) {
        // 关键修改：监听所有筛选器的变化事件（通过data-fieldname属性匹配所有筛选器）
        report.$wrapper.on("change", "[data-fieldname]", function() {
            // 触发报表刷新
            report.refresh();
        });

        // 图表渲染后优化标签显示
        report.$wrapper.on("chart-rendered", function() {
            if (!report.chart || !report.chart.instance) return;
            
            // 处理Y轴标签换行
            const yAxis = report.chart.instance.scales.y;
            if (yAxis && yAxis.ticks) {
                yAxis.ticks.forEach(tick => {
                    if (tick.label && tick.label.style) {
                        tick.label.style.whiteSpace = "normal";
                        tick.label.style.wordBreak = "break-all";
                        tick.label.style.maxWidth = "150px";
                    }
                });
                report.chart.instance.update();
            }
        });
    }
};